package com.syj.collect.adapter;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.syj.collect.app.R;
import com.syj.collect.entry.MyPhotoBean;
import com.syj.collect.listnner.MyItemListener;

import java.util.ArrayList;
import java.util.List;

public class MyPhotoAdapter extends RecyclerView.Adapter<MyPhotoAdapter.MyPhotoViewHoder> {


    private MyItemListener listenner;
    private List<MyPhotoBean.PhotoBean> pList = new ArrayList<>();

    public void setonClick(MyItemListener listener) {
        this.listenner = listener;
    }

    @NonNull
    @Override
    public MyPhotoAdapter.MyPhotoViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo, parent, false);
        return new MyPhotoViewHoder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyPhotoAdapter.MyPhotoViewHoder holder, int position) {
        holder.title.setText(pList.get(position).getName());
        holder.content.setText(pList.get(position).getAddress()+"");

        switch (pList.get(position).getUrls().size()) {
            case 3:
                Glide.with(holder.itemView.getContext()).load(pList.get(position).getUrls().get(2)).into(holder.img2);
            case 2:
                Glide.with(holder.itemView.getContext()).load(pList.get(position).getUrls().get(1)).into(holder.img1);
            case 1:
                Glide.with(holder.itemView.getContext()).load(pList.get(position).getUrls().get(0)).into(holder.img);
                break;
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listenner.onmListenner(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return pList.size();
    }

    public void upDate(List<MyPhotoBean.PhotoBean> pic) {
        pList.clear();
        pList.addAll(pic);
        notifyDataSetChanged();
    }


    /**
     *
     */
    class MyPhotoViewHoder extends RecyclerView.ViewHolder {
        private final ImageView img, img1, img2;
        private final TextView title, content;

        public MyPhotoViewHoder(@NonNull View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.myphoto_img);
            img1 = (ImageView) itemView.findViewById(R.id.myphoto_img1);
            img2 = (ImageView) itemView.findViewById(R.id.myphoto_img2);
            title = (TextView) itemView.findViewById(R.id.item_photo_title);
            content = (TextView) itemView.findViewById(R.id.item_photo_content);
        }
    }
}
